<?php
/**
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
use WordAnalysis\Analysis;
class Html extends Common
{
    public function index()
    {
    	h('phphtmlindexadmin');
        $id = input('id');
        $this->assign('id', $id);
        //输出模型
        $model = fun_sc_p('model', array('id' => $id), 'name');
        $this->assign('model', $model);
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $perpage = isset($_REQUEST['perpage']) ? $_REQUEST['perpage'] : 10;
        $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
        $tid = isset($_REQUEST['tid']) ? trim($_REQUEST['tid']) : 0;
        $open = isset($_REQUEST['open']) ? trim($_REQUEST['open']) : '';
        $settop = isset($_REQUEST['settop']) ? trim($_REQUEST['settop']) : '';
        $slide = isset($_REQUEST['slide']) ? trim($_REQUEST['slide']) : '';
        $startdate = isset($_REQUEST['startdate']) ? trim($_REQUEST['startdate']) : '';
        $enddate = isset($_REQUEST['enddate']) ? trim($_REQUEST['enddate']) : '';
        $map = [];
        if ($open != '') {
            $map['f.open'] = $open;
        }
        if ($settop != '') {
            $map['f.settop'] = $settop;
        }
        if ($slide != '') {
            $map['f.slide'] = $slide;
        }
        if ($keyword) {
            $map['f.title'] = ['like', "%{$keyword}%"];
        }
        if ($startdate) {
            $map['f.time'] = ['egt', strtotime($startdate)];
        }
        if ($enddate) {
            $map['f.time'] = ['elt', strtotime($enddate . ' 23:59:59')];
        }
        if ($tid == 0) {
            $html = Db::name($model)->alias('f')->join('cate c', 'c.id=f.tid')->field('f.*,c.id as cid')->order('f.times desc,f.id desc')->where($map)->paginate($perpage);
        } else {
            $html = Db::name($model)->alias('f')->join('cate c', 'c.id=f.tid')->field('f.*,c.id as cid')->where("f.tid={$tid} or c.tid={$tid}")->order('f.settop desc,f.id desc')->where($map)->paginate($perpage);
        }
        $this->assign(array('slide' => $slide, 'settop' => $settop, 'open' => $open, 'tid' => $tid, 'keyword' => $keyword, 'startdate' => $startdate, 'enddate' => $enddate, 'perpage' => $perpage));
        return $this->fetch('index', ['html' => $html]);
    }
    public function add()
    {
        $id = input('id');
        $this->assign('id', $id);
        $model = fun_sc_p('model', array('id' => $id), 'name');
        $this->assign('model', $model);
        return view();
    }
    public function edit()
    {
        $id = input('id');
        $model = input('model');
        $this->assign('id', $id);
        $this->assign('model', $model);
        $c = Db::name(input('model'))->where('id', input('id'))->find();
        $this->assign('c', $c);
        return view();
    }
    //添加
    public function insert()
    {
    	h('phphtmlinsertadmin');
        $data = $this->request->post();
        $data['time'] = time();
        $data['times'] = time();
        $data['model'] = fun_sc_p('cate', array('id' => input('tid')), 'model');
        if (empty(input('description'))) {
            $data['description'] = xss(htmltext(input('content')));
        }
        if (input('bdh')==1) {
        	$data['content'] = xss(getCurContent(input('content')));
        } else {
        	$data['content'] = xss(input('content'));
        }
        if (empty(input('pic'))) {
            $data['pic'] = Pic($data['content']);
        }
        $data['title'] = xss(input('title'));
        $content = preg_replace("/[a-z,A-Z]|[0-9]|[[:punct:]]/i", "", htmlspecialchars_decode(input('content')));
        if (empty(input('keywords'))) {
            $data['keywords'] = Analysis::getKeywords($content);
        } else {
        	$data['keywords'] = xss(input('keywords'));
        }
        $data['file'] = xss(input('file'));
        /*自定义字段过滤*/
        $wanneng = Db::name('modelZiduan')->where('tid', $data['model'])->select();
        foreach ($wanneng as $html) {
            $data[$html['name']] = xss(input($html['name']));
        }
        $name = fun_sc_p('model', array('id' => $data['model']), 'name');
        $html = Db::name($name);
        if (!$html->data($data)->strict(false)->insert()) {
            $this->error('添加失败');
        } else {
            //model_whole(上级,用户id,模型id);
            //fun_sc_p($name, array('time' => $data['time']), 'id');
            model_whole(fun_sc_p($name, array('time' => $data['time']), 'id'), input('uid'), $data['model']);
            $this->success('添加成功');
        }
    }
    //更新
    public function update()
    {
    	h('phphtmlupdateadmin');
        $id = input('id');
        $this->assign('id', $id);
        $data = $this->request->post();
        $data['times'] = time();
        $data['model'] = fun_sc_p('cate', array('id' => input('tid')), 'model');
        if (empty(input('description'))) {
            $data['description'] = xss(htmltext(input('content')));
        }
        if (input('bdh')==1) {
        	$data['content'] = xss(getCurContent(input('content')));
        } else {
        	$data['content'] = xss(input('content'));
        }
        if (empty(input('pic'))) {
            $data['pic'] = Pic($data['content']);
        }
        $data['title'] = xss(input('title'));
        $content = preg_replace("/[a-z,A-Z]|[0-9]|[[:punct:]]/i", "", htmlspecialchars_decode(input('content')));
        if (empty(input('keywords'))) {
            $data['keywords'] = Analysis::getKeywords($content);
        } else {
        	$data['keywords'] = xss(input('keywords'));
        }
        $data['file'] = xss(input('file'));
        $data['zan'] = 0;
        /*自定义字段过滤*/
        $wanneng = Db::name('modelZiduan')->where('tid', $data['model'])->select();
        foreach ($wanneng as $html) {
            $data[$html['name']] = xss(input($html['name']));
        }
        $name = fun_sc_p('model', array('id' => $data['model']), 'name');
        if (!Db::name($name)->strict(false)->data($data)->update($data)) {
            $this->error('修改失败');
        } else {
            $this->success('修改成功');
        }
    }
    //批量删除
    public function delss()
    {
    	h('phphtmldelssadmin');
        $id = input('id');
        //输出模型
        $model = fun_sc_p('model', array('id' => $id), 'name');
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = Db::name($model)->delete($ids);
        $arr = explode(',', $ids);
        for ($i = 0; $i < count($arr); $i++) {
            Db::name('model_whole')->where('tid', $arr[$i])->delete();
        }
        if ($result) {
            return json(array('code' => 200, 'msg' => '批量删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量删除失败'));
        }
    }
    //批量移动
    public function yidong()
    {
    	h('phphtmlyidongadmin');
        $id = input('id');
        if (empty(input('yidong'))) {
            return json(array('code' => 0, 'msg' => '请认真选择栏目id'));
        }
        //输出模型
        $model = fun_sc_p('model', array('id' => $id), 'name');
        $params = input('post.');
        $yidong['tid'] = $params['yidong'];
        $ids = implode(',', $params['ids']);
        $arr = explode(',', $ids);
        for ($i = 0; $i < count($arr); $i++) {
            $result = Db::name($model)->where('id', $arr[$i])->update($yidong);
        }
        if ($result) {
            return json(array('code' => 200, 'msg' => '批量移动成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量移动失败'));
        }
    }
}